package com.saturn.action.sldb.discussion;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.saturn.app.utils.DateUtils;
import com.saturn.app.web.IAction;
import com.saturn.app.web.IView;
import com.saturn.app.web.view.JspErrorView;
import com.saturn.app.web.view.JspView;
import com.saturn.auth.Organization;
import com.saturn.auth.User;
import com.saturn.sldb.Discussion;
import com.saturn.sldb.PersonState;
import com.saturn.sldb.Reconciliation;

public class ApplyDiscussionAction implements IAction {
	@Override
	public IView execute(HttpServletRequest request,
			HttpServletResponse response) {
		String id = request.getParameter("id");
		String type = request.getParameter("type"); // 获取要申报的类型

		String note = request.getParameter("note");
		if ("".equals(note) || note == null) {
			note = "";
		}
		User user = (User) request.getSession().getAttribute("authUser");
		User userOragantion = User.getOrganizationsByOrganizationId(user.getId());
		Discussion discussion = Discussion.get(id);

		PersonState personState = new PersonState(null, id, user.getId(),
				user.getName(), DateUtils.getSystemTime(),
				discussion.getName(), discussion.getIdentify(), "", note,
				discussion.getCreaterDepartment(), "一事一议");

		if ("1".equals(type)) { // 乡镇街-申报
			String files = discussion.getFiles();
			String[] filesStr = files.split(";");
			if (discussion.getMoney() != null
					&& "".equals(discussion.getMoney())
					&& !"".equals(discussion.getAudit())
					&& discussion.getAudit() != null) {
				return new JspView("/app/sldb/discussion/approval/error.jsp");
			} else if (filesStr.length < 5) {
				return new JspView(
						"/app/sldb/discussion/approval/errorFiles.jsp");
			} else {
				discussion.setState("待审批");
				if (Discussion.editToState(discussion) == 1) {

					personState.setState(discussion.getState());
					PersonState.add(personState);
					return new JspView("/app/sldb/discussion/apply/approval.jsp");
				} else {
					return new JspErrorView("信息更新失败");
				}
			}
		} else if ("2".equals(type)) {
			// 如果未上传附件则不能申请核对
			if (discussion.getFiles() != null
					&& "".equals(discussion.getFiles())) {
				return new JspView("/app/sldb/discussion/apply/nofiles.jsp");
			} else {
				discussion.setState("待核对");
				Discussion vo = Discussion.get(id);
				String inputType = vo.getInputType();
				if("自动".equals(inputType)){
					if (Discussion.editToState(discussion) == 1) {
						personState.setState(discussion.getState());
						PersonState.add(personState);
						return new JspView("/app/sldb/discussion/apply/autoShow.jsp");
					} else {
						return new JspErrorView("信息更新失败");
					}
				} else{
					if (Discussion.editToState(discussion) == 1) {
						personState.setState(discussion.getState());
						PersonState.add(personState);
						return new JspView("/app/sldb/discussion/apply/show.jsp");
					} else {
						return new JspErrorView("信息更新失败");
					}
				}
			}

		} else if ("3".equals(type)) { // 乡镇街临时专项付款
			discussion.setState("已付款");
			discussion.setSubject("临时专项");
			if (Discussion.editToState(discussion) == 1) {
				personState.setState(discussion.getState());
				PersonState.add(personState);
				return new JspView("/app/sldb/account/country.jsp");
			} else {
				return new JspErrorView("信息更新失败");
			}
		} else if ("11".equals(type)) { // 乡镇街结余调剂付款
			discussion.setState("已付款");
			discussion.setSubject("结余调剂");
			if (Discussion.editToState(discussion) == 1) {
				personState.setState(discussion.getState());
				PersonState.add(personState);
				return new JspView("/app/sldb/account/country.jsp");
			} else {
				return new JspErrorView("信息更新失败");
			}
		} else if ("4".equals(type)) {
			discussion.setState("未支付");
			if (discussion.getAudit() == null
					|| "".equals(discussion.getAudit())
					|| discussion.getMoney() == null
					|| "".equals(discussion.getMoney())) {
				return new JspView("/app/sldb/approval/discussion/error.jsp");
			} else {
				if (Discussion.editToState(discussion) == 1) {
					personState.setState(discussion.getState());
					PersonState.add(personState);
					return new JspView("/app/sldb/approval/discussion/show.jsp");
				}
			}

			return new JspErrorView("信息更新失败");

		} else if ("15".equals(type)) {
			discussion.setState("待付款");
			if (Discussion.editToState(discussion) == 1) {
				personState.setState(discussion.getState());
				PersonState.add(personState);
				return new JspView("/app/sldb/account/cancelByxz.jsp");
			} else {
				return new JspErrorView("信息更新失败");
			}
		} else if ("5".equals(type)) {
			discussion.setState("待审核");
			if (Discussion.editToState(discussion) == 1) {

				personState.setState(discussion.getState());
				PersonState.add(personState);
				return new JspView("/app/sldb/check/discussion/show.jsp");
			} else {
				return new JspErrorView("信息更新失败");
			}
		} else if ("6".equals(type)) {
			discussion.setState("未支付");
			if ("未支付".equals(Discussion.get(id).getState())) {
				return new JspView("/app/sldb/account/discussion/error.jsp");
			} else{
				String subject = discussion.getSubject();
				String organizationId = userOragantion.getOrganizationId();
				Organization organization = Organization.get(organizationId);
				String department = organization.getName();
				String typeStr = "一事一议";
				Reconciliation reg = Reconciliation.getBycondition(subject, department, typeStr);
				if("".equals(reg) || reg == null){
					return new JspView("/app/sldb/total/error.jsp");
				}else {
					//获取申报救助金额
					String money = discussion.getMoney();
					//支出金额减掉付款金额
					int moneyInt = Integer.parseInt(reg.getSurplus())-Integer.parseInt(money);
					String moneyStr = Integer.toString(moneyInt);
					reg.setSurplus(moneyStr);
					//当前总金额加上付款金额
					int totalInt = Integer.parseInt(reg.getTotal())+Integer.parseInt(money);
					String totalStr = Integer.toString(totalInt);
					reg.setTotal(totalStr);
					String total = reg.getTotal();
					String surplus = reg.getSurplus();
					int balance = Integer.parseInt(total)-Integer.parseInt(surplus);
					String balanceStr = Integer.toString(balance);
					reg.setBalance(balanceStr);
				if (Discussion.editToState(discussion) == 1) {
					personState.setState(discussion.getState());
					PersonState.add(personState);
					Reconciliation.edit(reg);
					return new JspView("/app/sldb/account/discussion/cancel.jsp");
				} else {
					return new JspErrorView("信息更新失败");
				}
			}
			}
		} else if ("7".equals(type)) {
			discussion.setState("已支付");
			if ("已支付".equals(Discussion.get(id).getState())) {
				return new JspView("/app/sldb/account/error.jsp");
			} else {
				discussion.setSubject("临时专项");
				String subject = discussion.getSubject();
				//String department = discussion.getCreaterDepartment();
				String organizationId = userOragantion.getOrganizationId();
				Organization organization = Organization.get(organizationId);
				String department = organization.getName();
				String typeStr = "一事一议";
				Reconciliation reg = Reconciliation.getBycondition(subject, department, typeStr);
				if("".equals(reg) || reg == null){
					return new JspView("/app/sldb/total/error.jsp");
				}else{
					String money = discussion.getMoney();
					int moneyInt = Integer.parseInt(money)+Integer.parseInt(reg.getSurplus());
					String moneyStr = Integer.toString(moneyInt);
					reg.setSurplus(moneyStr);
					String total = reg.getTotal();
					String surplus = reg.getSurplus();
					int balance = Integer.parseInt(total)-Integer.parseInt(surplus);
					String balanceStr = Integer.toString(balance);
					reg.setBalance(balanceStr);
				if (Discussion.editToState(discussion) == 1) {
					personState.setState(discussion.getState());
					PersonState.add(personState);
					Reconciliation.edit(reg);
					return new JspView("/app/sldb/account/discussion/show.jsp");
				} else {
					return new JspErrorView("信息更新失败");
				}
			}
			}
		} else if ("13".equals(type)) {
			discussion.setState("已支付");
			if ("已支付".equals(Discussion.get(id).getState())) {
				return new JspView("/app/sldb/account/error.jsp");
			} else {
				discussion.setSubject("结余调剂");
				String subject = discussion.getSubject();
				//String department = discussion.getCreaterDepartment();
				String organizationId = userOragantion.getOrganizationId();
				Organization organization = Organization.get(organizationId);
				String department = organization.getName();
				String typeStr = "一事一议";
				Reconciliation reg = Reconciliation.getBycondition(subject, department, typeStr);
				if("".equals(reg) || reg == null){
					return new JspView("/app/sldb/total/error.jsp");
				}else{
					String money = discussion.getMoney();
					int moneyInt = Integer.parseInt(money)+Integer.parseInt(reg.getSurplus());
					String moneyStr = Integer.toString(moneyInt);
					reg.setSurplus(moneyStr);
					String total = reg.getTotal();
					String surplus = reg.getSurplus();
					int balance = Integer.parseInt(total)-Integer.parseInt(surplus);
					String balanceStr = Integer.toString(balance);
					reg.setBalance(balanceStr);
				if (Discussion.editToState(discussion) == 1) {
					personState.setState(discussion.getState());
					PersonState.add(personState);
					Reconciliation.edit(reg);
					return new JspView("/app/sldb/account/discussion/show.jsp");
				} else {
					return new JspErrorView("信息更新失败");
				}
			}
			}
		} else if ("8".equals(type)) { // 乡镇街待付款
			discussion.setState("待付款");
			String files = discussion.getFiles();
			String[] filesStr = files.split(";");
			int money = Integer.parseInt(discussion.getMoney());
			if (money > 1000) {
				return new JspView(
						"/app/sldb/discussion/apply/errorByMoney.jsp");
			} else {
				if (filesStr.length < 5) {
					return new JspView(
							"/app/sldb/discussion/approval/errorFiles.jsp");
				}
				if (discussion.getMoney() == null
						|| "".equals(discussion.getMoney())
						|| discussion.getAudit() == null
						|| "".equals(discussion.getAudit())) {
					return new JspView(
							"/app/sldb/discussion/approval/error.jsp");
				} else {
					if (Discussion.editToState(discussion) == 1) {

						personState.setState(discussion.getState());
						PersonState.add(personState);
						return new JspView(
								"/app/sldb/discussion/approval/show.jsp");
					} else {
						return new JspErrorView("信息更新失败");
					}
				}
			}
		} else if ("9".equals(type)) { // 乡镇街待付款
			discussion.setState("已作废");
			if (Discussion.editToState(discussion) == 1) {

				personState.setState(discussion.getState());
				PersonState.add(personState);
				return new JspView("/app/sldb/discussion/apply/approval.jsp");
			} else {
				return new JspErrorView("信息更新失败");
			}
		} else if ("14".equals(type)) { // 民政局取消审批
			discussion.setState("待审批");
			if (Discussion.editToState(discussion) == 1) {

				personState.setState(discussion.getState());
				PersonState.add(personState);
				return new JspView("/app/sldb/approval/cancel/show.jsp");
			} else {
				return new JspErrorView("信息更新失败");
			}
		} else { // 民政局审批人员-驳回
			String remarks = request.getParameter("remarks");
			discussion.setState("创建");
			discussion.setRemarks(remarks);
			personState.setNote(remarks);
			if (Discussion.editToState(discussion) == 1) {
				personState.setState(discussion.getState());
				PersonState.add(personState);
				return new JspView("/app/sldb/approval/discussion/show.jsp");
			} else {
				return new JspErrorView("信息更新失败");
			}
		}
	}

	@Override
	public String requestMapping() {
		return "/app/sldb/discussion/applyDiscussion.action";
	}
}
